package com.sulent.frame.file.vfs.sftp;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.*;

public class SFTPChannel {
    static Session session = null;
    static Channel channel = null;


  	//登录sftp
    public  ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {
        String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
        String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
        String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
        String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);

        int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
        if (port != null && !port.equals("")) {
            ftpPort = Integer.valueOf(port);
        }

      	//创建JSch对象
        JSch jsch = new JSch();
      	//根据用户名，主机ip，端口获取一个Session对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
        if (ftpPassword != null) {
            session.setPassword(ftpPassword);
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        return (ChannelSftp) channel;
    }

  	//退出sftp
    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }


    public static void main(String[] args) throws Exception {
        Map<String, String> sftpDetails = new HashMap<String, String>();
        // 设置主机ip，端口，用户名，密码
        sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "192.168.138.100");
        sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "root");
        sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "centos");
        sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22");
        SFTPChannel sftpChannel = new SFTPChannel();
        ChannelSftp channel = sftpChannel.getChannel(sftpDetails, 60000);
        String pwd = channel.pwd();
        System.out.println(pwd);
        String path = "/usr/local";
        Vector ls = channel.ls("/usr/local");
        for (int i = 0; i < ls.size(); i++) {
            ChannelSftp.LsEntry o = (ChannelSftp.LsEntry) ls.get(i);
            String filename = o.getFilename();
            SftpATTRS attrs = o.getAttrs();
            boolean dir = attrs.isDir();
            System.out.print("文件名："+filename);
            System.out.print("   ");
            System.out.print("是否为目录："+dir);
            System.out.println();
            if (dir){
                String newPath = path +"/"+ filename;
                Vector ls1 = channel.ls(newPath);
                ChannelSftp.LsEntry o1 = (ChannelSftp.LsEntry) ls.get(i);
                SftpATTRS attrs1 = o1.getAttrs();
                System.out.print("文件名："+o1.getFilename());
                System.out.print("   ");
                System.out.print("是否为目录："+attrs1.isDir());
                System.out.println();
            }
            //System.out.println(o.toString());
        }
        sftpChannel.closeChannel();
    }
}
